package cn.edu.stu.contorller.backgroud;

import cn.edu.stu.bean.AjaxResult;
import cn.edu.stu.bean.MicUser;
import cn.edu.stu.service.RecordService;
import cn.edu.stu.service.SongService;
import cn.edu.stu.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Karl
 * @Date 2022/12/26 - 10:18
 */
@Controller
public class PageController {

    @Autowired
    UserService userService;

    @Autowired
    SongService songService;

    @Autowired
    RecordService recordService;

    @RequestMapping({"/bg/main","/"})
    public String main(){

        return "main";
    }

    @RequestMapping({"/bg/dynamic_table"})
    public String basic_table(
                              Model model,
                              @RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false,defaultValue = "5") Integer pageToShow){

        PageInfo<Map<String, Object>> page = songService.getSongByPage(pageNum, pageSize, pageToShow);
        convertDuration(page.getList());
        model.addAttribute("page",page);
        return "table/dynamic_table";
    }
    @RequestMapping({"/bg/editable_table"})
    public String editable_table(Model model,
                                 HttpServletRequest request,
                                 @RequestParam(value = "searchId",defaultValue = "0") Long searchId,
                                 @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                 @RequestParam(value = "pageToShow",defaultValue = "5")int pageToShow){

        long realId;
        if(searchId==0){
            realId = (long) request.getAttribute("userid");
        }else{
            realId = searchId;
        }
        PageInfo<Map<String, Object>> pageInfo = recordService.selectByUidWithPage(realId, pageNum, pageSize, pageToShow);
        convertDuration(pageInfo.getList());
        model.addAttribute("page",pageInfo);
        model.addAttribute("search",realId);
        return "table/editable_table";
    }
    @RequestMapping({"/bg/responsive_table"})
    public String responsive_table(HttpServletRequest request,
                                   Model model,
                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false,defaultValue = "5") Integer pageToShow){


        long uid = (long) request.getAttribute("userid");
        PageInfo<Map<String, Object>> pageInfo = recordService.selectByUidWithPage(uid, pageNum, pageSize, pageToShow);
        convertDuration(pageInfo.getList());
        model.addAttribute("page",pageInfo);
        return "table/responsive_table";
    }

    @RequestMapping({"/bg/basic_table"})
    public String dynamic_table(HttpServletRequest request,
            Model model,
            @RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false,defaultValue = "5") Integer pageToShow){
        PageInfo<MicUser> page = userService.getUserWithPage(pageNum, pageSize, pageToShow);

//        Long userId = (Long) request.getAttribute("userid");
//        if(userId!=1){
//            return "table/basic_table";
//        }
        model.addAttribute("users",page);
        return "table/basic_table";
    }

    public void convertDuration(List<Map<String, Object>> list){
        if(list!=null)
        for (Map<String, Object> map :list) {
            if(map.containsKey("duration")){
                int duration = (int) map.get("duration");
                map.put("duration",duration/60000+":"+duration/1000%60);
            }
        }
    }
}
